package com.fau.cm.action.index;

import java.io.BufferedOutputStream;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.fau.cm.action.PublicAction;
import com.fau.cm.entity.MedicalAdmin;
import com.fau.cm.entity.MedicalCallTaskStatistics;
import com.fau.cm.util.FileNameEncoderUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ModelDriven;
/**
 * @desc 员工任务统计Action 
 * @author kangyj
 * @datetime 2014年12月10日,上午11:10:54
 */
@Controller
@Scope("prototype")
@ParentPackage("default")
@Namespace("/")
public class StaffTaskStatisticsAction extends PublicAction implements
		ModelDriven<MedicalCallTaskStatistics> {

	private static final long serialVersionUID = -7995800438840722022L;

	private MedicalCallTaskStatistics taskStatistics = new MedicalCallTaskStatistics();
			
	/**
	 * @desc 员工任务页面初始化
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月10日,上午10:49:42
	 */
	@Action(value = "staff_task_init", results = {@Result(location = "staff_task_init.jsp")})
	public String staffTaskInit() {
		return SUCCESS;
	}

	/**
	 * @desc 员工任务查询
	 * @return
	 * @author kangyj
	 * @datetime 2014年12月10日,上午10:49:24
	 */
	@Action(value = "staff_task_view")
	public String staffTaskView() {
		HttpServletResponse response = ServletActionContext.getResponse();
		this.map = ActionContext.getContext().getParameters();
		MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		this.pageBean = this.baseAllService.getPerAnalysisManagerService().findStaffTaskStatistics(taskStatistics, admin, map);
		this.map.clear();
		this.map.put("total", this.pageBean.getTotalRow());
		this.map.put("data", this.pageBean.getResult());
		formatJSON(response, this.map);
		return null;
	}
	
	/**
	 * @desc 导出员工任务
	 * @return String
	 * @author liy
	 * @datetime 2014年12月13日,下午5:53:33
	 */
	@Action(value = "staff_task_export")
	public String export() {
		HttpServletResponse response = ServletActionContext.getResponse();
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			response.addHeader("Content-Disposition", "attachment;filename=" + FileNameEncoderUtil.encode(request,"员工任务.xls"));
			response.setContentType("application/vnd.ms-excel");
			OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
			MedicalAdmin admin = (MedicalAdmin) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
			Workbook workbook = this.baseAllService.getPerAnalysisManagerService().exportStaffTaskStatistics(admin, this.taskStatistics);
			workbook.write(toClient);
			toClient.flush();
			toClient.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	@Override
	public MedicalCallTaskStatistics getModel() {
		return taskStatistics;
	}

}
